package com.summer.data.parameter.blob;

import com.summer.data.parameter.BlobParameter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author 345480567@qq.com
 */
public class FileBlobParameter implements BlobParameter {

    private String fileName;

    private Integer len = 0;

    public FileBlobParameter(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void write(OutputStream outputStream) {
        try (FileInputStream inputStream = new FileInputStream(fileName)) {
            byte[] buffer = new byte[1024];
            int readlen;
            while ((readlen = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, readlen);
                len += readlen;
            }
        } catch (FileNotFoundException e) {
            // todo:
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "FileBlobParameter{}" + len;
    }
}
